package com.gpoint.rdp.door.zoneCTL.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.gpoint.rdp.base.core.util.string.StringUtil;
import com.gpoint.rdp.base.core.entity.ResultMessage;
import com.gpoint.rdp.base.framework.page.PageList;
import com.gpoint.rdp.base.framework.page.PageResult;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.gpoint.rdp.api.base.query.QueryFilter;
import com.gpoint.rdp.base.web.controller.GenericController;
import com.gpoint.rdp.base.web.json.PageJson;
import com.gpoint.rdp.base.web.util.RequestUtil;
import com.gpoint.rdp.base.web.util.RequestViewUtil;
import com.gpoint.rdp.door.PrivilegeOfPrivilegeType.persistence.dao.PrivilegeOfPrivilegeTypeDao;
import com.gpoint.rdp.door.TimeSegment.persistence.dao.TimeSegQueryDao;
import com.gpoint.rdp.door.TimeSegment.persistence.entity.TimeSegPo;
import com.gpoint.rdp.door.TimeSegment.service.TimeSegQueryService;
import com.gpoint.rdp.door.TimeSegment.service.TimeSegService;
import com.gpoint.rdp.door.area.persistence.dao.ZoneQueryDao;
import com.gpoint.rdp.door.area.persistence.entity.ZonePo;
import com.gpoint.rdp.door.zoneCTL.persistence.dao.DoorZoneDao;
import com.gpoint.rdp.door.zoneCTL.persistence.dao.DoorZoneQueryDao;
import com.gpoint.rdp.door.zoneCTL.persistence.entity.DoorZonePo;
import com.gpoint.rdp.door.zoneCTL.repository.DoorZoneRepository;
import com.gpoint.rdp.door.zoneCTL.service.DoorZoneQueryService;
import com.gpoint.rdp.door.zoneCTL.service.DoorZoneService;
import com.gpoint.rdp.door.zonepermission.persistence.dao.ZonePermissionQueryDao;
import com.gpoint.rdp.door.zonepermission.persistence.entity.ZonePermissionPo;

import cn.pluss.platform.shiro.DefaultPartyUserPo;
import cn.pluss.platform.shiro.SSORealm;

import java.util.ArrayList;
import java.util.HashMap;

import com.gpoint.rdp.base.core.util.BeanUtils;
import com.gpoint.rdp.base.core.util.JacksonUtil;

import java.util.List;
import java.util.Map;

// TODO: Auto-generated Javadoc
/**
 * TB_DOOR_ZONE 控制类
 *
 *<pre> 
 * 开发公司：苏州东大金点信息技术有限公司
 * 开发人员：唐红锋
 * 邮箱地址：55872655@qq.com
 * 创建时间：2019-09-24 15:58:34
 *</pre>
 */
@Controller
@RequestMapping("/rdp/zone/doorZone/")
public class CtlController extends GenericController{
	
	/** The door zone service. */
	@Resource
	private DoorZoneService doorZoneService;
	
	/** The door zone query service. */
	@Resource
	private DoorZoneQueryService doorZoneQueryService;
	
	/** The door zone query dao. */
//	@Resource
//	private  DoorZoneQueryDao doorZoneQueryDao ;
	
	/** The door zone dao. */
//	@Resource
//	private  DoorZoneDao doorZoneDao ;
//	
	/** The zone permission query dao. */
//	@Resource
//	private ZonePermissionQueryDao zonePermissionQueryDao;
	
	/** The zone query dao. */
//	@Resource
//	private ZoneQueryDao zoneQueryDao;
	
	
	/**
	 * 获取结构数据.
	 *
	 * @param request the request
	 * @param reponse the reponse
	 * @return the tree data
	 * @throws Exception the exception
	 */
	
	@RequestMapping("getTreeData")
	public @ResponseBody List<DoorZonePo> getTreeData(HttpServletRequest request,HttpServletResponse reponse) throws Exception{
		try {
			String listData = doorZoneQueryService.findAll();
			
			List<DoorZonePo> doorZoneList = DoorZonePo.fromJsonArrayString(listData);
			
			if(BeanUtils.isEmpty(doorZoneList)){
				doorZoneList=new ArrayList<DoorZonePo>();
			}
			/*构建TB_DOOR_ZONE根节点*/
			/*
			 * DoorZonePo doorZone=new DoorZonePo(); doorZone.setId("1");
			 * doorZone.setName("TB_DOOR_ZONE"); doorZone.setpId("0");
			 * doorZoneList.add(doorZone);
			 */
			return doorZoneList;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return null;
		}
	}
	
	/** The door zone repository. */
	@Resource
	private DoorZoneRepository doorZoneRepository;
	
	/**
	 * 编辑【TB_DOOR_ZONE】信息页面.
	 *
	 * @param request the request
	 * @param response the response
	 * @return the model and view
	 * @throws Exception the exception
	 */
	@RequestMapping("edit")
	public ModelAndView edit(HttpServletRequest request,HttpServletResponse response) throws Exception{

		String preUrl= RequestUtil.getPrePage(request);
		String id=RequestUtil.getString(request, "id");
		String name=RequestUtil.getString(request, "name");
		DoorZonePo doorZone=null;
		DoorZonePo zone=null;
		if(StringUtil.isNotEmpty(id)){
			String data = null;
			
//           if(id.equals("101")) { //会查询当前最新的vale 进行插入
//				id=
//			}
			//没有id 101 ，所以报错NULL
			data = doorZoneQueryService.get(id); 
			doorZone = DoorZonePo.fromJsonString(data);
//			从JSON中取出parentId
			 zone = new DoorZonePo();
			 if(null!=zone ) {
				    zone.setpId(doorZone.getpId());
					zone.setId(id);
					zone.setName(name);
					zone.setTid(doorZone.getTid());
			 }
//			 DoorZonePo po = doorZoneQueryDao.getByKey("get1", id);
//			 zone = new DoorZonePo();
//			 if(null!=zone ) {
//				    zone.setpId(po.getpId());
//					zone.setId(po.getId());
//					zone.setName(name);
//					zone.setTid(po.getTid());
//			 }
		    doorZoneRepository.newInstance(zone).update();
		}
           
		return new ModelAndView(RequestViewUtil.getViewUrl(request)).addObject("doorZone", zone).addObject("returnUrl", preUrl);
//		return getAutoView().addObject("doorZone", zone).addObject("returnUrl", preUrl);
	}
	
	/**
	 * 编辑【TB_DOOR_ZONE】信息页面.
	 *
	 * @param request the request
	 * @param response the response
	 * @return the model and view
	 * @throws Exception the exception
	 */
	@RequestMapping("flowEdit")
	public ModelAndView flowEdit(HttpServletRequest request,HttpServletResponse response) throws Exception{
		String preUrl= RequestUtil.getPrePage(request);
		String id=RequestUtil.getString(request, "id");
		DoorZonePo doorZone=null;
		if(StringUtil.isNotEmpty(id)){
			String data = null;
			data = doorZoneQueryService.get(id);
			doorZone = DoorZonePo.fromJsonString(data);
		}
		return getAutoView().addObject("doorZone", doorZone).addObject("returnUrl", preUrl);
	}
	
	/**
	 * 【TB_DOOR_ZONE】明细页面.
	 *
	 * @param request the request
	 * @param response the response
	 * @return the model and view
	 * @throws Exception the exception
	 */
	@RequestMapping("get")
	public ModelAndView get(HttpServletRequest request,HttpServletResponse response) throws Exception{
		String preUrl= RequestUtil.getPrePage(request);
		String id=RequestUtil.getString(request, "id");
		DoorZonePo doorZone=null;
		if(StringUtil.isNotEmpty(id)){
			String data = null;
			data = doorZoneQueryService.get(id);
			doorZone = DoorZonePo.fromJsonString(data);
		}
		return getAutoView().addObject("doorZone", doorZone).addObject("returnUrl", preUrl);
	}
	
	/**
	 *  
	 * 保存【TB_DOOR_ZONE】信息.
	 *
	 * @param json the json
	 * @param request the request
	 * @param response the response
	 * @throws Exception the exception
	 */
	@RequestMapping("save")
	public void save(@RequestBody String json,HttpServletRequest request,HttpServletResponse response) throws Exception{
		ResultMessage message=null;
		try {
              
			 //			doorZoneService.save( json);
			 DoorZonePo paramPo=DoorZonePo.fromJsonString(json);
			 this.doorZoneService.create(paramPo);
			 message=new ResultMessage(ResultMessage.SUCCESS, "保存TB_DOOR_ZONE成功");
		} catch (Exception e) {
			message=new ResultMessage(ResultMessage.FAIL, "对TB_DOOR_ZONE操作失败,"+e.getMessage());
			logger.error("对TB_DOOR_ZONE操作失败，" + e.getMessage(),e);
		}
		writeResultMessage(response.getWriter(), message);
	}
	
	/**
	 *  批量删除【TB_DOOR_ZONE】记录.
	 *
	 * @param request the request
	 * @param response the response
	 * @throws Exception the exception
	 */
	/*
	@RequestMapping("remove")
	public void remove(HttpServletRequest request,HttpServletResponse response) throws Exception{
		ResultMessage message=null;
		try {
	        
			
			 String pid=null;
			//得到id ，查询出PID
			String id=RequestUtil.getString(request, "id");
			//根据id,pid call sql ,得到记录ID
			String data = doorZoneQueryService.get(id); 
			DoorZonePo doorZone = DoorZonePo.fromJsonString(data);
				    pid = doorZone.getpId();
					id=doorZone.getId();
					Map hashmap=new HashMap(100);
					hashmap.put("pid", pid);
					hashmap.put("id", id);
			    List<DoorZonePo> polist = doorZoneQueryDao.findByKey("findpid",hashmap);
			  
			    if(polist.size()==1) {//单条直接删除
			    	
					 //获得待删除的id 
			    	 String[] ids=RequestUtil.getStringAryByStr(request, "id");
					 doorZoneService.deleteByIds(ids); 
					 message=new  ResultMessage(ResultMessage.SUCCESS, "删除TB_DOOR_ZONE成功");
					 
			    }else{
			    	   String[] idasArray=new String[polist.size()];
					   for (int i = 0; i < polist.size(); i++) {
						   idasArray[i]=polist.get(i).getId();
					   }
					        
					  //删除记录ID
						doorZoneService.deleteByIds(idasArray);
			    }
			   
		} catch (Exception e) {
			message=new ResultMessage(ResultMessage.FAIL, "删除TB_DOOR_ZONE失败，" + e.getMessage());
			logger.error("删除TB_DOOR_ZONE失败，" + e.getMessage(),e);
		}
		writeResultMessage(response.getWriter(), message);
	}
	 */
	/**
	 * List.
	 *
	 * @param request the request
	 * @param response the response
	 * @return the model and view
	 * @throws Exception the exception
	 */
	@RequestMapping("list")
	 public ModelAndView list(HttpServletRequest request,HttpServletResponse response) throws Exception{
	  ModelAndView mv=getAutoView();
	  return mv;
	  
	 }
	
	/**
	 * Tree.
	 *
	 * @param request the request
	 * @param response the response
	 * @return the model and view
	 * @throws Exception the exception
	 */
//	@RequestMapping("list2")
//	 public ModelAndView tree(HttpServletRequest request,HttpServletResponse response) throws Exception{
//	  ModelAndView mv= new ModelAndView("/rdp/zone/zTree.jsp") ;	  
//	  return mv;
//	  
//	 }
	
	/**
	 * Tree 3.
	 *
	 * @param request the request
	 * @param response the response
	 * @return the model and view
	 * @throws Exception the exception
	 */
	@RequestMapping("listboxselect")
	 public ModelAndView listboxselect(HttpServletRequest request,HttpServletResponse response) throws Exception{
	  ModelAndView mv= new ModelAndView("/rdp/zone/boxselect.jsp") ;	  
	  return mv;
	  
	 }
	
	/**
	 * Tree 4.
	 *
	 * @param request the request
	 * @param response the response
	 * @return the model and view
	 * @throws Exception the exception
	 */
	@RequestMapping("listuserPermissions")
	 public ModelAndView listuserPermissions(HttpServletRequest request,HttpServletResponse response) throws Exception{
	  ModelAndView mv= new ModelAndView("/rdp/zone/userPermission.jsp") ;	  
	  return mv;
	  
	 }
	
	/**
	 * Tree 5.
	 *
	 * @param request the request
	 * @param response the response
	 * @return the model and view
	 * @throws Exception the exception
	 */
	@RequestMapping("listuserPermission")
	 public ModelAndView listuserPermission(HttpServletRequest request,HttpServletResponse response) throws Exception{
		String type = request.getParameter("type");
	  ModelAndView mv= new ModelAndView("/rdp/zone/userPermission2.jsp") ;	  
	   mv.addObject("type",type);
	  return mv;
	  
	 }
	
	
	/**
	 * Tree 6.
	 *
	 * @param request the request
	 * @param response the response
	 * @return the model and view
	 * @throws Exception the exception
	 */
	@RequestMapping("listdoorpermission")
	 public ModelAndView listdoorpermission(HttpServletRequest request,HttpServletResponse response) throws Exception{
		String type = request.getParameter("type");
	  ModelAndView mv= new ModelAndView("/rdp/zone/doorpermission.jsp") ;	  
	   mv.addObject("type",type);
	  return mv;
	  
	 }
	

	/**
	 * listbatchAuth
	 *
	 * @param request the request
	 * @param response the response
	 * @return the model and view
	 * @throws Exception the exception
	 */
	@RequestMapping("listbatchAuth")
	 public ModelAndView listbatchAuth(HttpServletRequest request,HttpServletResponse response) throws Exception{
	
	  ModelAndView mv= new ModelAndView("/rdp/zone/batchAuth.jsp") ;	  
	   
	  return mv;
	  
	 }
	
	
	 /** The address. */
 	@Value("${watch.address}")
	 private  String address;
	
	/**
	 *  redicrect to parampanel jsp file.
	 *
	 * @param request the request
	 * @param response the response
	 * @return the model and view
	 * @throws Exception the exception
	 */
	@RequestMapping("list8")
	 public ModelAndView tree8(HttpServletRequest request,HttpServletResponse response) throws Exception{
	
	  ModelAndView mv= new ModelAndView("/rdp/monitoring/paramPanel.jsp") ;	  
	   mv.addObject("address", address);
	  return mv;
	  
	 }
	
	
	/**
	 * List 9.//门控制参数设置界面
	 *
	 * @param request the request
	 * @param response the response
	 * @return the model and view
	 * @throws Exception the exception
	 */
	
	@RequestMapping("listdoorParam")
	 public ModelAndView listdoorParam(HttpServletRequest request,HttpServletResponse response) throws Exception{
	
	  ModelAndView mv= new ModelAndView("/rdp/door/doorParam.jsp") ;	  
	   
	  return mv;
	  
	 }
	
	
	/**
	 * List 10.
	 *
	 * @param request the request
	 * @param response the response
	 * @return the model and view
	 * @throws Exception the exception
	 */
	//访问查看界面
//	@RequestMapping("list10")
//	 public ModelAndView list10(HttpServletRequest request,HttpServletResponse response) throws Exception{
//	
//	  ModelAndView mv= new ModelAndView("/rdp/door/doorParam.jsp") ;	  
//	   
//	  return mv;
//	  
//	 }
	
	
	/**
	 * List 11.
	 *
	 * @param request the request
	 * @param response the response
	 * @return the model and view
	 * @throws Exception the exception
	 */
	@RequestMapping("listfenpei")
	 public ModelAndView list11(HttpServletRequest request,HttpServletResponse response) throws Exception{
	
	 // ModelAndView mv= new ModelAndView("/rdp/area/zoneTreefenpei.jsp") ;	  
		String type = request.getParameter("type");
		 ModelAndView mv= new ModelAndView("/rdp/popup/departmentTree2.jsp") ;
		  mv.addObject("type",type);
	  return mv;
	  
	 }
	
	
	
	
	/**
	 * List 12.  生成区域权限管理界面
	 *
	 * @param request the request
	 * @param response the response
	 * @return the model and view
	 * @throws Exception the exception
	 */
	@RequestMapping("listZoneManage")
	 public ModelAndView listZoneManage(HttpServletRequest request,HttpServletResponse response) throws Exception{
	   
		//得到用户
		DefaultPartyUserPo   partyUserPo=SSORealm.queryCurrentShiroUser();
		System.out.println("Doorzone当前的用户：="+partyUserPo.getAccount());
		System.out.println(partyUserPo.getPassword());
		System.out.println(partyUserPo.getAddress());
		
		 String zoneid =null;
		 String account=partyUserPo.getAccount();
		// account="fdd";
		 String zoneids =null;
		  String zonename=null;
		  
		  //封装成list,前台用el 表达式然后遍历获取。
		  ArrayList<HashMap<String, String>> namelist=new ArrayList<HashMap<String, String>>();
		  
		  HashMap h=null;
		//得到用户所在的区域
		if(null!=account) {
//			List<ZonePermissionPo> list = zonePermissionQueryDao.findByKey("getZone", account);
			List<ZonePermissionPo> list = this.doorZoneQueryService.getZone(account);
			if(list.size()>0) {
				  for (int i = 0; i < list.size(); i++) {
					      zoneids = list.get(i).getZoneid();
					/*
					 * zoneids=zoneids.substring(zoneids.length()-1,zoneids.length()); //利用截取功能
					 * 到最后一位
					 * 
					 * //String[] zoneidArr = zoneids.split(","); // for (int j = 0; j <
					 * zoneidArr.length; j++) { // zoneid=zoneidArr[j]; //
					 * 
					 * //根据zoneid 查询zonename
					 * 
					 * if(zoneids!=null) { List<ZonePo> list2 = zoneQueryDao.findByKey("getname",
					 * zoneids); //只可能有一条记录 if(list2.size()>0) { zonename= list2.get(0).getName();
					 * h=new HashMap<String, String>(); h.put("zoneid", zoneids); //zoneid
					 * h.put("zonename", zonename); //zonename namelist.add(h); }
					 * 
					 * }
					 */
						
					      
//						}
				  }
			  }
			
		}
		
		//回显并隐藏 ，显示树的控件只支持单选的。改用下拉列表
		
		
		 ModelAndView mv= new ModelAndView("/rdp/zone/ZonebatchAuth.jsp") ;	  
		//跳转到网页赋值给树
	//	String str = JSON.toJSONString(namelist);
		//mv.addObject("namelist", str);
		  mv.addObject("zoneid", zoneids);
	  return mv;
	  
	 }
	
	
	
	
	
	/**
	 *  time segment assgin.
	 *
	 * @param request the request
	 * @param response the response
	 * @return the model and view
	 * @throws Exception the exception
	 */
	@RequestMapping("timeSegListDetail")
	 public ModelAndView timeSegListDetail(HttpServletRequest request,HttpServletResponse response) throws Exception{
	
	  ModelAndView mv= new ModelAndView("/rdp/TimeSegment/timeSegListDetail.jsp") ;	  
	   
	  return mv;
	  
	 }
	
	
	
	/**
	 *  user pid auth
	 *
	 * @param request the request
	 * @param response the response
	 * @return the model and view
	 * @throws Exception the exception
	 */
	@RequestMapping("listPidAuth")
	 public ModelAndView listPidAuth(HttpServletRequest request,HttpServletResponse response) throws Exception{
	
	  ModelAndView mv= new ModelAndView("/rdp/zone/pidAuth.jsp") ;	  
	   
	  return mv;
	  
	 }
	
	
	
	
	/**
	 * firstCardOpenDoor
	 *
	 * @param request the request
	 * @param response the response
	 * @return the model and view
	 * @throws Exception the exception
	 */
	@RequestMapping("firstCardOpenDoor")
	 public ModelAndView firstCardOpenDoor(HttpServletRequest request,HttpServletResponse response) throws Exception{
		 ModelAndView mv= new ModelAndView("/rdp/popup/departmentTree2.jsp") ;
    	  return mv;
	  
	 }
	
	
	
	/** The time seg service. */
	@Resource
	private TimeSegService timeSegService;
	
	/** The time seg query service. */
	@Resource
	private TimeSegQueryService timeSegQueryService;
	
	/** The time seg query dao. */
//	@Resource
//	private TimeSegQueryDao timeSegQueryDao;
//	
	
	/**
	 * Edits the 2.
	 *
	 * @param request the request
	 * @param response the response
	 * @return the model and view
	 * @throws Exception the exception
	 */
	@RequestMapping("timeSegDetail")
	public ModelAndView timeSegDetail(HttpServletRequest request,HttpServletResponse response) throws Exception{
		
		String id=RequestUtil.getString(request, "id");
		TimeSegPo timeSeg=null;
		if(StringUtil.isNotEmpty(id)){
//			String data = null;
//		
//			data = timeSegQueryService.get(id);
//			
//		
//			timeSeg = TimeSegPo.fromJsonString(data);
			
//			timeSeg= timeSegQueryDao.getByKey("get1",id);
	
			timeSeg=this.timeSegQueryService.findDetail(id);
		}
		ModelAndView mv = new ModelAndView("/rdp/TimeSegment/timeSegDetail.jsp");
		return mv.addObject("timeSeg", timeSeg);
	}
	
	
}